package org.geekbang.thinking.in.spring.resource.util;


import org.apache.commons.io.IOUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;

import java.io.IOException;
import java.io.Reader;

/**
 * {@link Resource} 工具类
 *
 * <p>
 * manager层接口及其实现应当准守符合微医开发规约，当前已集成参数校验规则，详情请见
 * <a href="https://gi.guahao.cn/docbook/service/validation.html">
 * </p>
 *
 * @author wangyh2
 * @version v1.0
 * @since 2021-10-24 16:06
 */
public interface ResourceUtils {
    static String getContent(Resource resource) {
        try {
            return getContent(resource, "UTF-8");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    static String getContent(Resource resource, String encoding) throws IOException {
        EncodedResource encodedResource = new EncodedResource(resource, encoding);
        // 字符输入流
        try (Reader reader = encodedResource.getReader()) {
            return IOUtils.toString(reader);
        }
    }
}
